package com.android.demo_20220829.manager;

import android.content.Context;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.manager.OkGoUtil;
import com.android.baselibrary.util.Util;
import com.lzy.okgo.model.HttpMethod;

import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/13.
 */

public class OkHttpManager {

    /**
     * Json格式通用接口请求，适用大部分接口
     *
     * @param context
     * @param methed      请求方式
     * @param url         请求地址
     * @param urlParam    url参数
     * @param headers     head参数
     * @param requestBody body参数
     * @param callBack
     */
    public static void okHttpRequest_Json(Context context, HttpMethod methed, String url, Map<String, Object> urlParam, Map<String, Object> headers, Map<String, Object> requestBody, OkHttpCallBack<?> callBack) {
        okHttpRequest(context, methed, url, urlParam, headers, requestBody, callBack, true);
    }

    public static void okHttpRequest(Context context, HttpMethod methed, String url, Map<String, Object> urlParam, Map<String, Object> headers, Map<String, Object> requestBody, OkHttpCallBack<?> callBack, boolean isJson) {
        //添加headers参数
        if (headers == null) {
            headers = new HashMap<>();
        }
        //token信息
//        if (UserManager.isLogin(context) && !headers.containsKey("Authorization")) {
//            headers.put("token",  UserManager.getToken(context));
//        } else {
//            headers.put("token", "");
//        }
        headers.put("token", UserManager.getToken(context));

        if (requestBody == null) {
            requestBody = new HashMap<>();
        }
        requestBody.put("appid", "MZT_APP");  // 固定值
        requestBody.put("appVersion", Util.getVersionName(context));  // app 版本
        requestBody.put("androidios", "android");  // 固定值

        OkGoUtil.okHttpRequest(context, methed, url, urlParam, headers, requestBody, callBack, isJson);
    }


    //文件上传
    public static void okHttpFileUpload(Context context, String url, Map<String, Object> headers, Map<String, Object> requestBody, File file, Type responseClass, OkHttpCallBack callBack) {
        //headers参数
        if (headers == null) {
            headers = new HashMap<>();
        }
//        if (UserManager.isLogin(context) && !headers.containsKey("Authorization")) {
//            headers.put("token",  UserManager.getToken(context));
//        } else {
//            headers.put("token", "");
//        }
        headers.put("token", UserManager.getToken(context));
        OkGoUtil.uploadByOkhttp(context, url, headers, requestBody, file, callBack);
    }


}
